﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Reflection;

namespace CH.Wallpaper.Download
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            cbWallpaperSource.DataSource = new List<KeyValuePair<string, string>> 
            {
                new KeyValuePair<string, string>("中关村","Zol|http://desk.zol.com.cn/"),
                new KeyValuePair<string, string>("太平洋","PConline|http://wallpaper.pconline.com.cn/"),
            };
            cbWallpaperSource.DisplayMember = "Key";
            cbWallpaperSource.ValueMember = "Value";
            wbBrowser.Navigate(cbWallpaperSource.SelectedValue.ToString().Split('|')[1]);
        }

        private void wbBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            
        }

        private void wbBrowser_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            wbBrowser.Navigate(wbBrowser.StatusText);
        }

        private void wbBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            string url = wbBrowser.Url.ToString();
            Downloader loader = (Downloader)Assembly.GetExecutingAssembly().CreateInstance("CH.Wallpaper.Download." + cbWallpaperSource.SelectedValue.ToString().Split('|')[0] + "Downloader");
            if (loader.IsAlbum(url))
            {
                txtUrl.Text = url;
                txtTitle.Text =loader.FilterTitle(wbBrowser.DocumentTitle);
                btnAddToDownloadList.Enabled = true;
            }
            else 
            {
                txtUrl.Text = "";
                txtTitle.Text = "";
                btnAddToDownloadList.Enabled = false;
            }
        }
    



        private void btnAddToDownloadList_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUrl.Text) || string.IsNullOrEmpty(txtTitle.Text))
            {
                MessageBox.Show("当前地址不可使用");
                return;
            }

            string item = cbWallpaperSource.SelectedValue.ToString().Split('|')[0] + "|" + txtTitle.Text + "|" + txtUrl.Text;
            if (!lbDownloadList.Items.Contains(item))
            {
                lbDownloadList.Items.Insert(0, item);
                lblMsg.Text = "添加成功";
            }
            else 
            {
                MessageBox.Show("已经存在下载列表!");
            }
        }

        private void btnGoHome_Click(object sender, EventArgs e)
        {
            wbBrowser.Navigate(cbWallpaperSource.SelectedValue.ToString().Split('|')[1]);
        }

        private void btnGoBack_Click(object sender, EventArgs e)
        {
            wbBrowser.GoBack();

        }

        private void btnGoForward_Click(object sender, EventArgs e)
        {
            wbBrowser.GoForward();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            wbBrowser.Refresh();
        }

        private void lbDownloadList_MouseHover(object sender, EventArgs e)
        {
            toolTip.SetToolTip(lbDownloadList, "双击即可删除!");
        }

        private void lbDownloadList_DoubleClick(object sender, EventArgs e)
        {
            if (lbDownloadList.Items.Count > 0) 
            {
                lbDownloadList.Items.Remove(lbDownloadList.SelectedItem);
            }
        }

        private void btnSelectSavePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult d = fbd.ShowDialog();
            if (d == DialogResult.OK) 
            {
                txtSavePath.Text = fbd.SelectedPath;
            }
        }

        private void btnStartDownload_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSavePath.Text)) 
            {
                MessageBox.Show("请选择保存路径");
                return;
            }
            btnStartDownload.Enabled = false;
            pbDownload.Minimum = 0;
            pbDownload.Maximum = lbDownloadList.Items.Count;
            foreach (var item in lbDownloadList.Items)
            {
          
                string[] array = item.ToString().Split('|');
                Downloader loader = (Downloader)Assembly.GetExecutingAssembly().CreateInstance("CH.Wallpaper.Download." + array[0] + "Downloader");
                loader.Resolution = cbResolution.SelectedItem.ToString();
                loader.SavePath = txtSavePath.Text + "\\" + array[1];;
                loader.URL = array[2];;
                loader.ItemDownloadCompleteEvent += new ItemDownloadCompleteDelegate(loader_ItemDownloadCompleteEvent);
                loader.Start();
            }

        }

     

        void loader_ItemDownloadCompleteEvent(string msg)
        {
            Invoke(new MethodInvoker(delegate
            {
                pbDownload.Value++;
                if (pbDownload.Value == pbDownload.Maximum)
                {
                    btnStartDownload.Enabled = true;
                    System.Diagnostics.Process.Start("explorer.exe", txtSavePath.Text);
                }
            }));
        }

        private void cbWallpaperSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            wbBrowser.Navigate(cbWallpaperSource.SelectedValue.ToString().Split('|')[1]);
            Downloader loader = (Downloader)Assembly.GetExecutingAssembly().CreateInstance("CH.Wallpaper.Download." + cbWallpaperSource.SelectedValue.ToString().Split('|')[0] + "Downloader");
            cbResolution.DataSource = loader.ResolutionList;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
            {
                Downloader loader = (Downloader)Assembly.GetExecutingAssembly().CreateInstance("CH.Wallpaper.Download." + cbWallpaperSource.SelectedValue.ToString().Split('|')[0] + "Downloader");
                cbResolution.DataSource = loader.ResolutionList;
            }
        }

        private void btnOpenFolder_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", txtSavePath.Text);
        }
    }
}
